<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/4/7
 * Time: 下午2:47
 */

namespace App\Admin\Controllers;
use App\Busi\Traits\DistTrait;
use App\Exceptions\UserException;
use App\Models\Album;
use App\Models\Dist;
use App\Models\SecQualifyCategory;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use App\Models\Company;
use App\Busi\Traits\CompanyTrait;
use App\Http\Controllers\Controller;
use League\Flysystem\Exception;

class CompanyController extends Controller
{
    use ModelForm,CompanyTrait,DistTrait;
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('公司列表');
            $content->description('');
            $content->body($this->grid());
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('新增公司');
            $content->description('');
            $content->body($this->form());
        });
    }
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑公司');
            $content->description('');
            $content->body($this->form('edit')->edit($id));
        });
    }

    protected function grid() {
        return Admin::grid(Company::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->name('公司名');
            $obj = $this;
            $grid->province_id('省')->display(function ($province_id)use($obj){
                return $obj->getDistNamePid($province_id);
            });
            $grid->city_id('市')->display(function ($city_id)use($obj){
                return $obj->getDistNamePid($city_id);
            });
            $grid->name('公司名');
            $grid->address('地址');
            $grid->scale('规模');
            $grid->account('注册账号');
            $grid->subcategorys("关联资质")->display(function ($subcategorys){
                $subcategorys = array_map(function ($subcategory) {
                    return "<a class='success' href='secqualifycate/".$subcategory['id']."' target='_blank'>{$subcategory['category_name']}</a><br>";
                }, $subcategorys);

                return join('', $subcategorys);
            });
            $grid->logo('logo')->display(function ($logo){
                $width = 40;
                $height = 40;
                if(strpos($logo, 'http')===false) {
                    $logo = config('admin.upload.host').$logo;
                }
                return "<img src='{$logo}' width='{$width}' height='{$height}'/>";
            });
            $grid->licence_poster('营业执照')->display(function ($licence_poster){
                $width = 40;
                $height = 40;
                if(strpos($licence_poster, 'http')===false) {
                    $licence_poster = config('admin.upload.host').$licence_poster;
                }
                return "<img src='{$licence_poster}' width='{$width}' height='{$height}'/>";
            });
            $grid->star("星级");
            $grid->view_num("查看次数");

            $grid->flag('审核状态')->display(function($flag){
                if( $flag==0 ) return '未认证';
                if( $flag==1 ) return '未审核';
                if( $flag==2 ) return '审核不通过';
                return '审核通过';
            });
            $grid->created_at('创建时间');
            $grid->filter(function ($filter) {
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {

            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
//                    $actions->disableDelete();
                });
            });
        });
    }

    protected function form($mode='')
    {
        return Admin::form(Company::class, function (Form $form) use($mode){
            if($mode=='edit'){
                $form->display('id','ID');
                $form->display('created_at', '创建时间');
                $form->display('updated_at', '更新时间');
            }
            $form->text('name','企业名');
            $form->text('desc','公司介绍');
            $form->select('province_id',"省")->options(Dist::where("parent_id",-1)->pluck("dist_name","id"))->load('city_id', '/api/city');
            $form->select("city_id","市")->options(function (){
                $province_id = $this->province_id;
                return Dist::where("parent_id",$province_id)->pluck('dist_name','id');
            });
//            $form->select('city_id',"市");
            $form->text('address','公司地址');
            $form->text('scale','公司规模');
            $form->text('contact_number','联系电话');
            $form->text('advantage','公司优势');
            $form->text('account','注册账号');
            $form->multipleSelect('subcategorys','关联资质')->options(SecQualifyCategory::all()->pluck('category_name', 'id'));
            $form->text('star','星级');
            $form->text('view_num','查看次数');
            $form->text('show_order','展示顺序越小越靠前');

            $form->image('logo','企业logo')->uniqueName()->removable();


            $form->image('licence_poster','营业执照')->uniqueName()->removable();
            $form->multipleImage('pictures','公司图册')->removable()->uniqueName();
            $form->radio('flag', '审核状态')->options([0=>'未认证', 1=>'未审核',2=>'审核不通过',3=>'审核通过']);
        });
    }
}